'use strict';
const dbHelper = require('db-helper')
const uniID = require('uni-id')
const {uniIDConfigMerchant} = require('config')

const getUniID = (context) => {
	const uniIDIns = uniID.createInstance({
		context,
		config: uniIDConfigMerchant
	})
	return uniIDIns
}


exports.main = async (event, context) => {
	//event为客户端上传的参数
	console.log('event: ', event)
	let { queryStringParameters } = event
	if (!queryStringParameters) {
		return {
			code: 500,
			msg: '参数不正确'
		}
	}
	let { id, token } = queryStringParameters
	
	if (!token) {
		return {
			code: 500,
			msg: 'token不存在'
		}
	}
	let payload = await getUniID(this.ctx.context).checkToken(token)
	if (payload.code) {
		return {
			code: 500,
			msg: 'token已过期'
		}
	}
	if (!id) {
		return {
			code: 500,
			msg: 'id不存在'
		}
	}
	
	try{
		let script = await dbHelper.getById("t3ds-script", id)
		if (!script) {
			return {
				code: 500,
				msg: "剧本不存在"
			}
		}
		return {
			code: 200,
			data: script.detail || ''
		}
	}catch(e){
		console.error("获取剧本详情失败", e);
		return {
			code: 500,
			msg: e.message
		}
	}
};
